var express = require('express');
var router = express.Router();
const dataCtrl = require('../controllers/release.controller');
const multiparty = require('multiparty');
const fs = require('fs');
// rest api get/post/put/delete



router.post('/data', dataCtrl.create);
router.get('/data/:id', dataCtrl.getData);
router.put('/data/:id', dataCtrl.update);
router.post('/list', dataCtrl.list);
router.delete('/data/:id', dataCtrl.remove);
router.post('/data/removes', dataCtrl.removes);
router.post('/uploadImg', function(req, res, next) {
    var form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.maxFilesSize = 2 * 1024 * 1024;
    form.uploadDir = './../uploadtemp';

    form.parse(req, function(err, fields, files) {

        var uploadurl = './images/uploads/';
        file = files['filedata'];
        originalFilename = file[0].originalFilename; //原始文件名
        tmpPath = file[0].path; //原始的路径

        var timestamp = new Date().getTime(); //时间戳
        uploadurl += timestamp + originalFilename;
        newPath = './public/' + uploadurl;


        var fileReadStream = fs.createReadStream(tmpPath);
        var fileWriteStream = fs.createWriteStream(newPath);
        fileReadStream.pipe(fileWriteStream);
        fileWriteStream.on('close', () => {
            fs.unlinkSync(tmpPath);
            res.send('{"err":"","msg":"' + uploadurl + '"}');
        })
    });


});

// router.post('/', dataCtrl.create);
module.exports = router;